BLOCK.Model = "models/Combine_turrets/Floor_turret.mdl";
BLOCK.BlockName = "Turret";
BLOCK.Desc = "Will fire at nearby foes";
BLOCK.Health = 500;
BLOCK.HackTime = 5;
BLOCK.HackDuration = 10;
BLOCK.FireRadiusMax = 400;
BLOCK.FireAngleMax = 90;
BLOCK.MaxTurrets = 5;

if(!CLIENT) then

function BLOCK:Init()
	self.hackTeam = self.Team;
	self.recoil = 0;
	self.lastThink = CurTime();
	self:ConVar("FireRadius", 200);
	self:ConVar("FireAngle", 45);
	self.hackTeam = self.Team;
end

function BLOCK:Think()
	if(self.recoil <= 0) then
		self:Fire();
	else
		local elapsed = CurTime() - self.lastThink;
		self.recoil = self.recoil - elapsed;
	end
	self.lastThink = CurTime();
end

function BLOCK:Fire()
	local ang = self:ConVarGet("FireAngle");
	local range = self:ConVarGet("FireRadius");
	local entities = ents.FindInSphere(self.SENT:GetPos(), range);
	for key, val in pairs(entities) do
		if(val:IsPlayer() && val:Alive() && val:Team() != self.hackTeam) then
			local dis = val:GetPos() - self.SENT:GetPos();
			local angles = dis:Angle();
			if(angles.y < ang || angles.y > 360-ang) then
				local bullet = {};
				local gun = self.SENT:GetAttachment(1)
				bullet.Src = gun.Pos;
				bullet.Attacker = self.SENT;
			
				bullet.Dir = dis:Normalize();
				bullet.Spread = Vector(0, 0, 0);
				bullet.Num = 1;
				bullet.Damage = 10;
				bullet.Force = 1;
				bullet.Tracer = 1;
				bullet.TraceName = "Tracer";
				self.SENT:FireBullets(bullet);
				self.recoil = 0.2;
				return;
			end
		end
	end
end

function BLOCK:Hack( ply )
	self.hackTeam = ply:Team();
end

function BLOCK:EndHack()
	self.hackTeam = self.Team;
end

function BLOCK:ValidPos(vec, err, ply)
	if(err) then
		local blocks = ents.FindByClass("bh_block");
		local builders = ents.FindByClass("bh_builder");
		table.Add(blocks, builders);
		local turret_counter = 0;
		for key, val in pairs(blocks) do
			if(val.blockName == "turret" && ply:Team() == val:GetTeam()) then 
				turret_counter = turret_counter + 1; 
			end
		end
		if(turret_counter >= self.MaxTurrets) then
			hook.Call("ShowNotice", nil, "Too many turrets!", ply);
			return false;
		end
	end
	return true;
end

else

local Laser = Material("cable/redlaser");

function BLOCK:Render()
	local radius = self:ConVarGet("FireRadius");
	local ang = self:ConVarGet("FireAngle");
	local ang1 = Angle(0,ang,0);
	local vec1 = ang1:Forward();
	vec1 = vec1 * radius;
	local vector = self.SENT.Entity:LocalToWorld(vec1);
	
	local ang2 = Angle(0,360-ang,0);
	local vec2 = ang2:Forward();
	vec2 = vec2 * radius;
	local vector2 = self.SENT.Entity:LocalToWorld(vec2);
	
	render.SetMaterial( Laser);
	render.DrawBeam(self.SENT.Entity:GetPos(), vector, 5, 0, 0, Color(255, 255, 255, 255));
	render.DrawBeam(self.SENT.Entity:GetPos(), vector2, 5, 0, 0, Color(255, 255, 255, 255));
	return true;
end

function BLOCK:SetupValues(val)
	local rangeRatio = val;
	local angleRatio = 1 - val;
	local range = rangeRatio * self.FireRadiusMax;
	local angle = angleRatio * self.FireAngleMax;
	
	self:ConVar("FireRadius", range);
	self:ConVar("FireAngle", angle);
end

function BLOCK:DrawCPanel( p )
	local slide = p:NumSlider("Angle-Range", nil, 0, 1, 2);
	local range = self:ConVarGet("FireRadius") / self.FireRadiusMax;
	//local angle = self:ConVarGet("FireAngle") / self.FireAngleMax;
	slide:SetValue( range );
	local this = self;
	function slide:OnValueChanged(val) this:SetupValues(val); end
	
	return true;
end
end